import requests
import urllib3
import os
from config import base_url

urllib3.disable_warnings()

folder = f"{base_url}/resource"


def download_file(data_list):
    if not os.path.exists(folder):
        os.mkdir(folder)

    def download(elem):
        file_name, *_, url = elem
        r = requests.get(url=url, stream=True, verify=False)
        with open(f"{folder}/{file_name}", "wb") as f:
            for chunk in r.iter_content(chunk_size=512):
                if chunk:
                    f.write(chunk)
                    f.flush()

    total = len(data_list)
    count = 1
    for elm in data_list:
        download(elm)
        print(f"已完成{count}/{total}")
        count += 1
